package ch07.home;

class Exercise7_19 {
	public static void main(String args[]) {
		Buyer b = new Buyer();
		b.buy(new Tv19());
		b.buy(new Computer());
		b.buy(new Tv19());
		b.buy(new Audio());
		b.buy(new Computer());
		b.buy(new Computer());
		b.buy(new Computer());
		b.summary();
	}
}

class Buyer {
	int money = 1000;
	Product19[] cart = new Product19[3];
	int i = 0;

	void buy(Product19 p) {
		if (money < p.price) {
			System.out.println("잔액이 부족하여 " + p + "을/를 살수 없습니다.");
			return;
		}
		money -= p.price;
		add(p);
	}

	void add(Product19 p) {
		if (i >= cart.length) {
			Product19[] tmp = new Product19[cart.length * 2];
			System.arraycopy(cart, 0, tmp, 0, cart.length);
			cart = tmp;
		}
		cart[i++] = p;
	}

	void summary() {
		String itemList = "";
		int sum = 0;
		for (int i = 0; i < cart.length; i++) {
			if (cart[i] == null) break;
			itemList += cart[i] + " ";
			sum += cart[i].price;
		}
		
		System.out.println("구입한 물건:" + itemList);
		System.out.println("사용한 금액:" + sum);
		System.out.println("남은 금액:" + money);
	}
}

class Product19 {
	int price; // 제품의 가격

	Product19(int price) {
		this.price = price;
	}
}

class Tv19 extends Product19 {
	Tv19() {
		super(100);
	}

	public String toString() {
		return "Tv";
	}
}

class Computer extends Product19 {
	Computer() {
		super(200);
	}

	public String toString() {
		return "Computer";
	}
}

class Audio extends Product19 {
	Audio() {
		super(50);
	}

	public String toString() {
		return "Audio";
	}
}
/*
잔액이 부족하여 Computer을/를 살수 없습니다.
구입한 물건:Tv Computer Tv Audio Computer Computer 
사용한 금액:850
남은 금액:150
*/